package com.sduept.nwld.dataserver.controller.fault;

import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.bigdata.fault.entity.*;
import com.sduept.bigdata.fault.enums.DeviceActionType;
import com.sduept.bigdata.fault.manager.*;
import com.sduept.bigdata.fault.model.DeviceActionProcessDTO;
import com.sduept.cim.model.extend.LineSegment;
import com.sduept.cim.model.protection.ProtectionEquipment;
import com.sduept.cim.model.wires.Breaker;
import com.sduept.cim.query.service.LineTowerQueryService;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.core.entity.SystemParameter;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.PrimeFaces;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.*;

@Named
@ViewScoped
public class AcLineFaultReportController extends FaultReportEditAbstractController {

	private static final long serialVersionUID = -5494341361896590389L;
	@Autowired
	private SuperDAO dao;
	@Autowired
	private CimManager cm;
	@Autowired
	private FaultReportPersistManager fpm;
	@Autowired
	private FaultRecordPersistManager frpm;
	@Autowired
	private FaultReportQueryManager fqm;
	@Autowired
	private FaultRecordQueryManager frqm;
	@Autowired
	private ComponentDictionaryManager cdm;
	@Autowired
	private FaultEditLockManager lockM;
	@Autowired
	private ParamManager cacheM;
	@Autowired
	private PersonnelManager personM;
	@Autowired
	private LineTowerQueryService lineTowerQueryService;
	@Autowired
	private FaultExtendManager fem;
	private static final String zbh = "主保护";
	private static final String dlqbh = "断路器保护";
	private static final String dlq = "断路器";
	private FaultReportMain faultReportMain = new FaultReportMain();
	private FaultReportStationDetail firstStationDetail;
	private FaultReportStationDetail secondStationDetail;
	// 第一个站的保护设备动作详情
	private List<DeviceActionProcessDTO> firstProtectDeviceActionProcessList = new ArrayList<>();
	// 第二个站的保护设备动作详情
	private List<DeviceActionProcessDTO> secondProtectDeviceActionProcessList = new ArrayList<>();
	// 第一个站的断路器设备动作详情
	private List<DeviceActionProcessDTO> firstBreakerDeviceActionProcessList = new ArrayList<>();
	// 第二个站的断路器设备动作详情
	private List<DeviceActionProcessDTO> secondBreakerDeviceActionProcessList = new ArrayList<>();

	// 第一个站的动作元件
	private List<FaultReportComponentDetail> firstComponentList = new ArrayList<>();
	// 第二个站的动作元件
	private List<FaultReportComponentDetail> secondComponentList = new ArrayList<>();
	// 第一个站的动作元件副本
	private List<FaultReportComponentDetail> copyFirstComponentList = new ArrayList<>();
	// 第二个站的动作元件副本
	private List<FaultReportComponentDetail> copySecondComponentList = new ArrayList<>();
	private String currentSelectStation = null;
	private boolean showFirstTableAllColumns = false;
	private boolean showSecondTableAllColumns = false;
	private Comparator<FaultReportComponentDetail> componentComparator = null;
	private Comparator<DeviceActionProcessDTO> processComparator = null;
	// 第一个站选择的设备动作详情
	private List<DeviceActionProcessDTO> firstSelectedProtectDeviceActionProcessList;
	// 第二个站选择的设备动作详情
	private List<DeviceActionProcessDTO> secondSelectedProtectDeviceActionProcessList;
	// 第一个站选择的设备动作详情
	private List<DeviceActionProcessDTO> firstSelectedBreakerDeviceActionProcessList;
	// 第二个站选择的设备动作详情
	private List<DeviceActionProcessDTO> secondSelectedBreakerDeviceActionProcessList;

	// 第一个站选择的动作元件
	private List<FaultReportComponentDetail> firstSelectComponentList;
	// 第二个站选择的动作元件
	private List<FaultReportComponentDetail> secondSelectComponentList;
	// 当前选择的动作元件
	private FaultReportComponentDetail currentSelectComponentDetail;

	private FaultRecord fr = null;
	private boolean firstStationEdit;
	private boolean secondStationEdit;

	/**
	 * 初始化故障信息
	 * 
	 * 将FaultRecord查询由使用SuperDAO改为使用 FaultRecordQueryManager查询 （20190109 zhangqi）
	 */
	@PostConstruct
	private void init() {
		SystemParameter parameter = cacheM.get(ParamDesc.FAULT_REPORT_EDIT_INTERVAL);
		if (parameter != null && StringUtils.isNotEmpty(parameter.getValue())) {
			timingInteval = Integer.parseInt(parameter.getValue());
		}
		initBackButton();
		componentComparator = new Comparator<FaultReportComponentDetail>() {
			@Override
			public int compare(FaultReportComponentDetail o1, FaultReportComponentDetail o2) {
				if (o1.getActionTime() > o2.getActionTime()) {
					return 1;
				} else if (o1.getActionTime() == o2.getActionTime()) {
					return 0;
				} else {
					return -1;
				}
			}
		};
		processComparator = new Comparator<DeviceActionProcessDTO>() {
			@Override
			public int compare(DeviceActionProcessDTO o1, DeviceActionProcessDTO o2) {
				if (zbh.equals(o1.getDevType()))
					return -1;
				else if (dlqbh.equals(o1.getDevType())) {
					if (dlq.equals(o2.getDevType()))
						return -1;
					else if (zbh.equals(o2.getDevType()))
						return 1;
					else
						return 0;
				} else
					return 1;
			}
		};
		faultId = getParameter("faultId");
		if (StringUtils.isNotEmpty(faultId)) {
			fr = frqm.findById(faultId);
			faultReportMain = fqm.getFaultReportMain(faultId);
			if (faultReportMain == null || StringUtils.isEmpty(fr.getStationId())) {
				LineSegment line = cm.getLineSegmentById(fr.getEquipment_id());
				if (line != null) {
					fr.setStationId(line.getStartST());
					fr.setStationName(line.getStartStName());
					fr.setStationId2(line.getEndST());
					fr.setStationName2(line.getEndStName());
				}
				frpm.saveFaultRecord(new FaultRecordWrapper(false, false, fr));
				// 重新获取一下faultReportMain
				faultReportMain = fqm.getFaultReportMain(faultId);
			}
			for (FaultReportStationDetail sdetail : faultReportMain.getFaultReportStationDetails()) {
				if (StringUtils.isNotEmpty(fr.getStationId()) && fr.getStationId().equals(sdetail.getStationId())) {
					firstStationDetail = sdetail;
					if (firstStationDetail.getFirstFault() != null)
						firstStationDetail.setFirstFaultMs(firstStationDetail.getFirstFault() / 1000f);// 设置初次故障时间
					if (firstStationDetail.getRecloseFault() != null)
						firstStationDetail.setRecloseFaultMs(firstStationDetail.getRecloseFault() / 1000f);// 设置合于故障时间

					currentSelectStation = firstStationDetail.getStationId();
					firstComponentList.addAll(firstStationDetail.getFaultReportComponentDetails());
					initDeviceActionProcessData(firstStationDetail, firstProtectDeviceActionProcessList,
							firstBreakerDeviceActionProcessList);
					Collections.sort(firstComponentList, componentComparator);
					copyFirstComponentList = copyComponentDetails(firstComponentList);
					unionComponentDetails(fr, firstComponentList, firstProtectDeviceActionProcessList,
							firstBreakerDeviceActionProcessList);
				} else if (StringUtils.isNotEmpty(fr.getStationId2())
						&& fr.getStationId2().equals(sdetail.getStationId())) {
					secondStationDetail = sdetail;
					if (secondStationDetail.getFirstFault() != null)
						secondStationDetail.setFirstFaultMs(secondStationDetail.getFirstFault() / 1000f);
					if (secondStationDetail.getRecloseFault() != null)
						secondStationDetail.setRecloseFaultMs(secondStationDetail.getRecloseFault() / 1000f);

					secondComponentList.addAll(secondStationDetail.getFaultReportComponentDetails());
					initDeviceActionProcessData(secondStationDetail, secondProtectDeviceActionProcessList,
							secondBreakerDeviceActionProcessList);
					Collections.sort(secondComponentList, componentComparator);
					copySecondComponentList = copyComponentDetails(secondComponentList);
					unionComponentDetails(fr, secondComponentList, secondProtectDeviceActionProcessList,
							secondBreakerDeviceActionProcessList);
				}
			}
			if (firstStationDetail != null) {
				stationId1 = firstStationDetail.getStationId();
			}
			if (secondStationDetail != null) {
				stationId2 = secondStationDetail.getStationId();
			}
			loadAttachmentList(faultReportMain.getFaultRecord(), stationId1, stationId2);
		}
	}

	/**
	 * 更新资源锁时间戳
	 */
	public void pushTiming() {
		if (firstStationEdit) {
			lockM.gainLock(faultId + firstStationDetail.getId(), new Date().getTime(), getCurrentUser().getId());
		}
		if (secondStationEdit) {
			lockM.gainLock(faultId + secondStationDetail.getId(), new Date().getTime(), getCurrentUser().getId());
		}
	}

	/**
	 * 元件动作详情，保护名称选择事件
	 *
	 * @param event
	 */
	public void handleProtectSelect(SelectEvent event) {
		Object item = event.getObject();
		if (item == null || currentSelectComponentDetail == null) {
			return;
		}
		// 设置保护类型，保护型号
		if (item.toString().startsWith("主")) {// 主保护
			currentSelectComponentDetail.setProtectType("线路保护");
			if (currentSelectStation.equals(firstStationDetail.getStationId())) {
				for (DeviceActionProcessDTO dto : firstProtectDeviceActionProcessList) {
					if (StringUtils.isNotEmpty(dto.getDevName())
							&& dto.getDevName().equals(currentSelectComponentDetail.getProtectName())) {
						currentSelectComponentDetail.setProtectModel(dto.getDevModel());
						currentSelectComponentDetail.setPtId(dto.getId());
						break;
					}
				}
			} else if (currentSelectStation.equals(secondStationDetail.getStationId())) {
				for (DeviceActionProcessDTO dto : secondProtectDeviceActionProcessList) {
					if (StringUtils.isNotEmpty(dto.getDevName())
							&& dto.getDevName().equals(currentSelectComponentDetail.getProtectName())) {
						currentSelectComponentDetail.setProtectModel(dto.getDevModel());
						currentSelectComponentDetail.setPtId(dto.getId());
						break;
					}
				}
			}
		} else {// 断路器保护
			currentSelectComponentDetail.setProtectType("断路器保护");
			if (currentSelectStation.equals(firstStationDetail.getStationId())) {
				for (DeviceActionProcessDTO dto : firstProtectDeviceActionProcessList) {
					if (StringUtils.isNotEmpty(currentSelectComponentDetail.getProtectName())
							&& StringUtils.isNotEmpty(dto.getDevName()) && dto.getDevName()
									.contains(currentSelectComponentDetail.getProtectName().replace("断路器保护", ""))) {
						currentSelectComponentDetail.setProtectModel(dto.getDevModel());
						currentSelectComponentDetail.setPtId(dto.getId());
						break;
					}
				}
			} else if (currentSelectStation.equals(secondStationDetail.getStationId())) {
				for (DeviceActionProcessDTO dto : secondProtectDeviceActionProcessList) {
					if (StringUtils.isNotEmpty(currentSelectComponentDetail.getProtectName())
							&& StringUtils.isNotEmpty(dto.getDevName()) && dto.getDevName()
									.contains(currentSelectComponentDetail.getProtectName().replace("断路器保护", ""))) {
						currentSelectComponentDetail.setProtectModel(dto.getDevModel());
						currentSelectComponentDetail.setPtId(dto.getId());
						break;
					}
				}
			}
		}
		// 设置备注信息
		String faultType = faultReportMain.getFaultRecord().getFaultType();
		if (StringUtils.isNotEmpty(faultType)) {
			if (!"单永".equals(faultType)) {
				currentSelectComponentDetail.setRemark("故障初");
			}
		}
	}

	/**
	 * 动作元件列表，行选择事件
	 *
	 * @param event
	 */
	public void onActionComponentRowSelect(SelectEvent event) {
		currentSelectComponentDetail = (FaultReportComponentDetail) event.getObject();
	}

	/**
	 * 根据元件型号动加载该型号对应的元件信息
	 *
	 * @param query
	 * @return
	 */
	public List<String> completeActionComponent(FaultReportComponentDetail query) {
		List<String> results = new ArrayList<String>();
		// 设置该型号对应的动作元件
		List<ComponentDictionary> list = cdm.findComponents(query.getProtectModel(), true);
		if (list.size() == 0) {
			String name = query.getProtectName();
			if (StringUtils.isEmpty(name)) {
				return results;
			}
			if (name.startsWith("主")) {
				results = cdm.findMainProtectActionComponents();
			} else {
				results = cdm.findBreakerProtectActionComponents();
			}
		} else {
			for (ComponentDictionary df : list) {
				results.add(df.getActionComponent());
			}
		}
		return results;
	}

	/**
	 * 根据元件型号动加载该型号对应的元件信息
	 *
	 * @param query
	 * @return
	 */
	public List<String> completeActionCondition(String query) {
		List<String> results = new ArrayList<String>();
		String faultPhase = faultReportMain.getFaultRecord().getFaultPhase();
		if (StringUtils.isEmpty(faultPhase)) {
			results.add("三跳");
		} else if (faultPhase.length() == 1) {
			results.add("跳" + faultPhase + "相");
			results.add("重合" + faultPhase + "相");
			results.add("三跳");
		} else {
			results.add("三跳");
		}
		return results;
	}

	/**
	 * 动作元件选择监听事件
	 *
	 * @param event
	 */
	public void handleComponentSelect(SelectEvent event) {
		if (currentSelectComponentDetail == null)
			return;
		String pmodel = currentSelectComponentDetail.getProtectModel();
		String acomponet = (String) event.getObject();
		String funClassification = cdm.findfunClassification(pmodel, acomponet);
		if (StringUtils.isEmpty(funClassification)) {
			if (StringUtils.isNotEmpty(currentSelectComponentDetail.getProtectName())
					&& !currentSelectComponentDetail.getProtectName().startsWith("主"))
				currentSelectComponentDetail.setFunClassification("断路器保护");
		} else {
			currentSelectComponentDetail.setFunClassification(funClassification);
		}
		// 设置动作情况
		String faultType = faultReportMain.getFaultRecord().getFaultType();
		String faultPhase = faultReportMain.getFaultRecord().getFaultPhase();
		if (StringUtils.isEmpty(faultType)) {
			return;
		} else if ("单瞬".equals(faultType)) {
			if (acomponet.contains("重合")) {
				currentSelectComponentDetail.setActionCondition("重合" + faultPhase + "相");
			} else {
				currentSelectComponentDetail.setActionCondition("跳" + faultPhase + "相");
			}
		} else if ("单永".equals(faultType)) {
			if (acomponet.contains("重合")) {
				currentSelectComponentDetail.setActionCondition("重合" + faultPhase + "相");
				currentSelectComponentDetail.setRemark("故障初");
			}
		} else {// 其他故障
			currentSelectComponentDetail.setActionCondition("三跳");
		}
	}

	/**
	 * 保存故障报告信息
	 */
	public void saveReportMain() {
		try {
			String faultPhase = fr.getFaultPhase();
			List<FaultReportMainProtection> fmainList = generateFaultReportMainProtection(
					firstProtectDeviceActionProcessList);
			List<FaultReportMainProtection> smainList = generateFaultReportMainProtection(
					secondProtectDeviceActionProcessList);
			List<FaultReportBreakerProtection> fbpList = generateFaultReportBreakerProtection(faultPhase,
					firstProtectDeviceActionProcessList);
			List<FaultReportBreakerProtection> sbpList = generateFaultReportBreakerProtection(faultPhase,
					secondProtectDeviceActionProcessList);
			List<FaultReportBreaker> fbList = generateFaultReportBreaker(faultPhase,
					firstBreakerDeviceActionProcessList);
			List<FaultReportBreaker> sbList = generateFaultReportBreaker(faultPhase,
					secondBreakerDeviceActionProcessList);
			StringBuilder firstSb = new StringBuilder("");
			if (firstStationDetail != null) {
				// 设置行波测距
				if (firstStationDetail.getTravelingWaveDouble() != null
						&& firstStationDetail.getTravelingWaveDouble() > 0) {
					firstSb = new StringBuilder(firstStationDetail.getStationName() + "："
							+ firstStationDetail.getTravelingWaveDouble() + "km");
				} else if (firstStationDetail.getTravelingWave() != null && firstStationDetail.getTravelingWave() > 0) {
					firstSb = new StringBuilder(
							firstStationDetail.getStationName() + "：" + firstStationDetail.getTravelingWave() + "km");
				}
				if (firstStationDetail.getFirstFaultMs() != null) {
					firstStationDetail.setFirstFault((int) (firstStationDetail.getFirstFaultMs() * 1000));// 由于页面显示的是毫秒,数据库存入的是微妙所以需要扩大1000倍
				}
				if (firstStationDetail.getRecloseFaultMs() != null) {
					firstStationDetail.setRecloseFault((int) (firstStationDetail.getRecloseFaultMs() * 1000));
				}
			}
			StringBuilder secondSb = new StringBuilder("");
			if (secondStationDetail != null) {
				// 设置行波测距
				if (secondStationDetail.getTravelingWaveDouble() != null
						&& secondStationDetail.getTravelingWaveDouble() > 0) {
					secondSb.append(secondStationDetail.getStationName()).append("：")
							.append(secondStationDetail.getTravelingWaveDouble() + "km");
				} else if (secondStationDetail.getTravelingWave() != null
						&& secondStationDetail.getTravelingWave() > 0) {
					secondSb.append(secondStationDetail.getStationName()).append("：")
							.append(secondStationDetail.getTravelingWave() + "km");
				}
				if (secondStationDetail.getFirstFaultMs() != null) {
					secondStationDetail.setFirstFault((int) (secondStationDetail.getFirstFaultMs() * 1000));
				}
				if (secondStationDetail.getRecloseFaultMs() != null) {
					secondStationDetail.setRecloseFault((int) (secondStationDetail.getRecloseFaultMs() * 1000));
				}
			}
			Set<FaultReportStationDetail> set = new HashSet<>();
			if (firstStationDetail != null) {
				// 清除可能过期的数据
				firstStationDetail.getFaultReportBreakerActions().clear();
				firstStationDetail.getFaultReportBreakerProtectionActions().clear();
				// firstStationDetail.getFaultReportComponentDetails().clear();
				firstStationDetail.getFaultReportMainProtectionActions().clear();
				set.add(firstStationDetail);
			}
			if (secondStationDetail != null) {
				// 清除可能过期的数据
				secondStationDetail.getFaultReportBreakerActions().clear();
				secondStationDetail.getFaultReportBreakerProtectionActions().clear();
				// secondStationDetail.getFaultReportComponentDetails().clear();
				secondStationDetail.getFaultReportMainProtectionActions().clear();
				set.add(secondStationDetail);
			}
			if (set.size() > 0)
				faultReportMain.setFaultReportStationDetails(set);
			faultReportMain = fpm.saveFaultReportMain(faultReportMain);

			fpm.saveFaultReportMainActionInfos(firstStationDetail, fmainList);
			fpm.saveFaultReportMainActionInfos(secondStationDetail, smainList);

			fpm.saveFaultReportBreakerProtectActionInfos(firstStationDetail, fbpList);
			fpm.saveFaultReportBreakerProtectActionInfos(secondStationDetail, sbpList);

			fpm.saveFaultReportBreakerActionInfos(firstStationDetail, fbList);
			fpm.saveFaultReportBreakerActionInfos(secondStationDetail, sbList);

			listCompareAndSave(firstComponentList, copyFirstComponentList);
			listCompareAndSave(secondComponentList, copySecondComponentList);
			fpm.saveFaultReportComponentDetail(firstStationDetail, firstComponentList);
			fpm.saveFaultReportComponentDetail(secondStationDetail, secondComponentList);
			String range = firstSb.append("\n").append(secondSb.toString()).toString();
			if (StringUtils.isEmpty(range)) {
				fr.setRanging(null);
			} else {
				fr.setRanging(range);
			}
//			1.在保存的时候，调用getTowerNo()方法
//			2.返回一个interger类型
			List<Double> distances = getStartAndEndDistances(firstStationDetail, secondStationDetail);
			Integer distanse = lineTowerQueryService.getTowerNo(distances.get(0), distances.get(1),
					fr.getEquipment_id());
//			3.set进faultReport 的startPole
			fr.setStartPole(distanse);
//			4.set 故障过程,如何保证不覆盖掉用户自己编写的动作过程？//TODO
			/*if(fr.getDescStatus()==0) {
				String desc = fem.getFaultRecordprocessDesc(fr);
				fr.setDescription(desc);
			}*/
			frpm.saveFaultRecordOnly(fr);
			//异步更新故障源元件统计信息
			ayncUpdateActionUploadInfo(fr.getId());
			addSuccessMessage("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			addErrorMessage("保存失败，请重新打开再次尝试保存！");
		}
	}

	public List<String> completeProtectName(String query) {
		List<String> result = new ArrayList<String>();
		result.add("主一保护");
		result.add("主二保护");
		result.add("主三保护");
		String eid = faultReportMain.getFaultRecord().getEquipment_id();
		if (StringUtils.isNotEmpty(eid)) {
			List<Breaker> list = null;
			if (firstStationDetail != null && currentSelectStation.equals(firstStationDetail.getStationId())) {
				list = cm.getBreakersById(eid, firstStationDetail.getStationId());
			} else if (secondStationDetail != null && currentSelectStation.equals(secondStationDetail.getStationId())) {
				list = cm.getBreakersById(eid, secondStationDetail.getStationId());
			}
			if (list != null) {
				for (Breaker b : list) {
					result.add(b.getName() + "断路器保护");
				}
			}
		}
		return result;
	}

	/**
	 * 重置故障报告填写人
	 */
	public void resetreportPersonAndUnit() {
		if (StringUtils.isNotEmpty(currentSelectStation)) {
			if (currentSelectStation.equals(firstStationDetail.getStationId())) {
				firstStationDetail.setReportPerson(null);
				firstStationDetail.setReportUnit(null);
			} else if (currentSelectStation.equals(secondStationDetail.getStationId())) {
				secondStationDetail.setReportPerson(null);
				secondStationDetail.setReportUnit(null);
			}
		}
	}

	/**
	 * 重置故障报告审核人
	 */
	public void resetreportAuditor() {
		if (StringUtils.isNotEmpty(currentSelectStation)) {
			if (currentSelectStation.equals(firstStationDetail.getStationId())) {
				firstStationDetail.setReviewer(null);
			} else if (currentSelectStation.equals(secondStationDetail.getStationId())) {
				secondStationDetail.setReviewer(null);
			}
		}
	}

	/**
	 * 校验列是否显示
	 *
	 * @param actionTime actionTime 第几次动作第一次传入first，重合闸传入reclose，第二次动作传入second
	 * @param phase      当前列代表的相别
	 * @return
	 */
	public boolean isShow(String actionTime, String phase) {
		if (showFirstTableAllColumns && currentSelectStation.equals(firstStationDetail.getStationId()))
			return true;
		else if (showSecondTableAllColumns && currentSelectStation.equals(secondStationDetail.getStationId()))
			return true;
		FaultRecord fr = faultReportMain.getFaultRecord();
		if (StringUtils.isEmpty(fr.getFaultPhase()) || StringUtils.isEmpty(fr.getFaultType()))
			return true;
		boolean flag = false;
		if (fr.getFaultPhase().length() > 1 && "first".equals(actionTime))
			flag = true;
		else if (fr.getFaultPhase().length() == 1) {
			if ("单瞬".equals(fr.getFaultType()) && (phase.equals(fr.getFaultPhase()) && "first".equals(actionTime)
					|| "reclose".equals(actionTime))) {
				flag = true;
			} else if ("单永".equals(fr.getFaultType())) {
				// 单永分两种 目前只涉及正常单永
				if (phase.equals(fr.getFaultPhase()) && "first".equals(actionTime) || "reclose".equals(actionTime)
						|| "second".equals(actionTime))
					return true;
			}
		}
		return flag;
	}

	public List<String> completeActionComponentRemark(String query) {
		List<String> results = new ArrayList<String>();
		results.add("故障初");
		results.add("重合后");
		return results;
	}

	/**
	 * 添加动作元件
	 */
	public void addActionComponent() {
		FaultReportComponentDetail fd = new FaultReportComponentDetail();
		fd.setId(ObjectId.get().toString());
		fd.setStatus(1);
		if (firstStationDetail != null && currentSelectStation.equals(firstStationDetail.getStationId())) {
			firstComponentList.add(fd);
			// Collections.sort(firstComponentList,componentComparator);
		} else if (secondStationDetail != null && currentSelectStation.equals(secondStationDetail.getStationId())) {
			secondComponentList.add(fd);
			// Collections.sort(secondComponentList,componentComparator);
		}
	}

	private List<FaultReportMainProtection> generateFaultReportMainProtection(List<DeviceActionProcessDTO> list) {
		List<FaultReportMainProtection> result = new ArrayList<>();
		for (DeviceActionProcessDTO d : list) {
			if (zbh.equals(d.getDevType())) {
				if (d.getfTripA() >= 0) {
					FaultReportMainProtection main = new FaultReportMainProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("A");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripA() * 1000));
					result.add(main);
				}
				if (d.getfTripB() >= 0) {
					FaultReportMainProtection main = new FaultReportMainProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("B");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripB() * 1000));
					result.add(main);
				}
				if (d.getfTripC() >= 0) {
					FaultReportMainProtection main = new FaultReportMainProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("C");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripC() * 1000));
					result.add(main);
				}
				if (d.getsTripA() >= 0) {
					FaultReportMainProtection main = new FaultReportMainProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("A");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripA() * 1000));
					result.add(main);
				}
				if (d.getsTripB() >= 0) {
					FaultReportMainProtection main = new FaultReportMainProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("B");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripB() * 1000));
					result.add(main);
				}
				if (d.getsTripC() >= 0) {
					FaultReportMainProtection main = new FaultReportMainProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("C");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripC() * 1000));
					result.add(main);
				}
			}
		}
		return result;
	}

	private List<FaultReportBreakerProtection> generateFaultReportBreakerProtection(String faultPhase,
			List<DeviceActionProcessDTO> list) {
		List<FaultReportBreakerProtection> result = new ArrayList<>();
		for (DeviceActionProcessDTO d : list) {
			if (dlqbh.equals(d.getDevType())) {
				if (d.getfTripA() >= 0) {
					FaultReportBreakerProtection main = new FaultReportBreakerProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("A");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripA() * 1000));
					result.add(main);
				}
				if (d.getfTripB() >= 0) {
					FaultReportBreakerProtection main = new FaultReportBreakerProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("B");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripB() * 1000));
					result.add(main);
				}
				if (d.getfTripC() >= 0) {
					FaultReportBreakerProtection main = new FaultReportBreakerProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("C");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripC() * 1000));
					result.add(main);
				}
				if (d.getRecolse() >= 0) {
					FaultReportBreakerProtection main = new FaultReportBreakerProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase(faultPhase);
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.reclose.toString());
					main.setDistance0MomentsTime((int) (d.getRecolse() * 1000));
					result.add(main);
				}
				if (d.getsTripA() >= 0) {
					FaultReportBreakerProtection main = new FaultReportBreakerProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("A");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripA() * 1000));
					result.add(main);
				}
				if (d.getsTripB() >= 0) {
					FaultReportBreakerProtection main = new FaultReportBreakerProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("B");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripB() * 1000));
					result.add(main);
				}
				if (d.getsTripC() >= 0) {
					FaultReportBreakerProtection main = new FaultReportBreakerProtection();
					main.setProtectionId(d.getId());
					main.setProtectionName(d.getDevName());
					main.setPhase("C");
					main.setDevType(d.getDevModel());
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripC() * 1000));
					result.add(main);
				}
			}
		}
		return result;
	}

	private List<FaultReportBreaker> generateFaultReportBreaker(String faultPhase, List<DeviceActionProcessDTO> list) {
		List<FaultReportBreaker> result = new ArrayList<>();
		for (DeviceActionProcessDTO d : list) {
			if (dlq.equals(d.getDevType())) {
				if (d.getfTripA() >= 0) {
					FaultReportBreaker main = new FaultReportBreaker();
					main.setBreakerId(d.getId());
					main.setBreakerName(d.getDevName());
					main.setPhase("A");
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripA() * 1000));
					result.add(main);
				}
				if (d.getfTripB() >= 0) {
					FaultReportBreaker main = new FaultReportBreaker();
					main.setBreakerId(d.getId());
					main.setBreakerName(d.getDevName());
					main.setPhase("B");
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripB() * 1000));
					result.add(main);
				}
				if (d.getfTripC() >= 0) {
					FaultReportBreaker main = new FaultReportBreaker();
					main.setBreakerId(d.getId());
					main.setBreakerName(d.getDevName());
					main.setPhase("C");
					main.setStatus(1);
					main.setActionType(DeviceActionType.trip.toString());
					main.setDistance0MomentsTime((int) (d.getfTripC() * 1000));
					result.add(main);
				}
				if (d.getRecolse() >= 0) {
					FaultReportBreaker main = new FaultReportBreaker();
					main.setBreakerId(d.getId());
					main.setBreakerName(d.getDevName());
					main.setPhase(faultPhase);
					main.setStatus(1);
					main.setActionType(DeviceActionType.reclose.toString());
					main.setDistance0MomentsTime((int) (d.getRecolse() * 1000));
					result.add(main);
				}
				if (d.getsTripA() >= 0) {
					FaultReportBreaker main = new FaultReportBreaker();
					main.setBreakerId(d.getId());
					main.setBreakerName(d.getDevName());
					main.setPhase("A");
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripA() * 1000));
					result.add(main);
				}
				if (d.getsTripB() >= 0) {
					FaultReportBreaker main = new FaultReportBreaker();
					main.setBreakerId(d.getId());
					main.setBreakerName(d.getDevName());
					main.setPhase("B");
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripB() * 1000));
					result.add(main);
				}
				if (d.getsTripC() >= 0) {
					FaultReportBreaker main = new FaultReportBreaker();
					main.setBreakerId(d.getId());
					main.setBreakerName(d.getDevName());
					main.setPhase("C");
					main.setStatus(1);
					main.setActionType(DeviceActionType.threeTrip.toString());
					main.setDistance0MomentsTime((int) (d.getsTripC() * 1000));
					result.add(main);
				}
			}
		}
		return result;
	}

	/**
	 * 初始化设备动作流程table
	 *
	 * @param detail
	 * @param processList
	 */
	private void initDeviceActionProcessData(FaultReportStationDetail detail,
			List<DeviceActionProcessDTO> protectProcessList, List<DeviceActionProcessDTO> breakerProcessList) {
		Map<String, DeviceActionProcessDTO> map = new HashMap<>();
		Set<FaultReportMainProtection> set = detail.getFaultReportMainProtectionActions();
		for (FaultReportMainProtection main : set) {
			DeviceActionProcessDTO dto = map.get(main.getProtectionId());
			if (dto == null) {
				dto = new DeviceActionProcessDTO();
				dto.setId(main.getProtectionId());
				dto.setDevModel(main.getDevType());
				dto.setDevName(main.getProtectionName());
				dto.setDevType(zbh);
				map.put(main.getProtectionId(), dto);
				protectProcessList.add(dto);
			}
			if ("A".equals(main.getPhase())) {
				if (DeviceActionType.trip.toString().equals(main.getActionType()))
					dto.setfTripA(main.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(main.getActionType()))
					dto.setsTripA(main.getDistance0MomentsTime() / 1000f);
			} else if ("B".equals(main.getPhase())) {
				if (DeviceActionType.trip.toString().equals(main.getActionType()))
					dto.setfTripB(main.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(main.getActionType()))
					dto.setsTripB(main.getDistance0MomentsTime() / 1000f);
			} else if ("C".equals(main.getPhase())) {
				if (DeviceActionType.trip.toString().equals(main.getActionType()))
					dto.setfTripC(main.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(main.getActionType()))
					dto.setsTripC(main.getDistance0MomentsTime() / 1000f);
			}
		}
		String eid = faultReportMain.getFaultRecord().getEquipment_id();
		if (set.size() == 0) {
			List<ProtectionEquipment> mainList = cm.listMainEquipmentsByStationIdAndprotectedId(detail.getStationId(),
					eid);
			for (ProtectionEquipment pe : mainList) {
				DeviceActionProcessDTO dto = map.get(pe.getId());
				if (dto == null) {
					dto = new DeviceActionProcessDTO();
					dto.setId(pe.getId());
					dto.setDevModel(pe.getDevType());
					dto.setDevName(pe.getType() != null ? pe.getType() : pe.getName());
					dto.setDevType(zbh);
					protectProcessList.add(dto);
				}
			}
		}
		Set<FaultReportBreakerProtection> bpset = detail.getFaultReportBreakerProtectionActions();
		for (FaultReportBreakerProtection bp : bpset) {
			DeviceActionProcessDTO dto = map.get(bp.getProtectionId());
			if (dto == null) {
				dto = new DeviceActionProcessDTO();
				dto.setId(bp.getProtectionId());
				dto.setDevModel(bp.getDevType());
				dto.setDevName(bp.getProtectionName());
				dto.setDevType(dlqbh);
				map.put(bp.getProtectionId(), dto);
				protectProcessList.add(dto);
			}
			if ("A".equals(bp.getPhase())) {
				if (DeviceActionType.trip.toString().equals(bp.getActionType()))
					dto.setfTripA(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.reclose.toString().equals(bp.getActionType()))
					dto.setRecolse(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(bp.getActionType()))
					dto.setsTripA(bp.getDistance0MomentsTime() / 1000f);
			} else if ("B".equals(bp.getPhase())) {
				if (DeviceActionType.trip.toString().equals(bp.getActionType()))
					dto.setfTripB(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.reclose.toString().equals(bp.getActionType()))
					dto.setRecolse(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(bp.getActionType()))
					dto.setsTripB(bp.getDistance0MomentsTime() / 1000f);
			} else if ("C".equals(bp.getPhase())) {
				if (DeviceActionType.trip.toString().equals(bp.getActionType()))
					dto.setfTripC(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.reclose.toString().equals(bp.getActionType()))
					dto.setRecolse(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(bp.getActionType()))
					dto.setsTripC(bp.getDistance0MomentsTime() / 1000f);
			}
		}
		if (bpset.size() == 0) {
			List<ProtectionEquipment> bpList = cm.getBreakerProtectByPrimaryIdAndStationId(eid, detail.getStationId());
			for (ProtectionEquipment pe : bpList) {
				DeviceActionProcessDTO dto = map.get(pe.getId());
				if (dto == null) {
					dto = new DeviceActionProcessDTO();
					dto.setId(pe.getId());
					dto.setDevModel(pe.getDevType());
					dto.setDevName(pe.getName());
					dto.setDevType(dlqbh);
					protectProcessList.add(dto);
				}
			}
		}
		Set<FaultReportBreaker> bset = detail.getFaultReportBreakerActions();
		for (FaultReportBreaker bp : bset) {
			DeviceActionProcessDTO dto = map.get(bp.getBreakerId());
			if (dto == null) {
				dto = new DeviceActionProcessDTO();
				dto.setId(bp.getBreakerId());
				dto.setDevName(bp.getBreakerName());
				dto.setDevType(dlq);
				map.put(bp.getBreakerId(), dto);
				breakerProcessList.add(dto);
			}
			if ("A".equals(bp.getPhase())) {
				if (DeviceActionType.trip.toString().equals(bp.getActionType()))
					dto.setfTripA(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.reclose.toString().equals(bp.getActionType()))
					dto.setRecolse(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(bp.getActionType()))
					dto.setsTripA(bp.getDistance0MomentsTime() / 1000f);
			} else if ("B".equals(bp.getPhase())) {
				if (DeviceActionType.trip.toString().equals(bp.getActionType()))
					dto.setfTripB(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.reclose.toString().equals(bp.getActionType()))
					dto.setRecolse(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(bp.getActionType()))
					dto.setsTripB(bp.getDistance0MomentsTime() / 1000f);
			} else if ("C".equals(bp.getPhase())) {
				if (DeviceActionType.trip.toString().equals(bp.getActionType()))
					dto.setfTripC(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.reclose.toString().equals(bp.getActionType()))
					dto.setRecolse(bp.getDistance0MomentsTime() / 1000f);
				else if (DeviceActionType.threeTrip.toString().equals(bp.getActionType()))
					dto.setsTripC(bp.getDistance0MomentsTime() / 1000f);
			}
		}
		if (bset.size() == 0) {
			List<Breaker> bList = cm.getBreakersById(eid, detail.getStationId());
			for (Breaker b : bList) {
				DeviceActionProcessDTO dto = map.get(b.getId());
				if (dto == null) {
					dto = new DeviceActionProcessDTO();
					dto.setId(b.getId());
					dto.setDevName(b.getName());
					dto.setDevType(dlq);
					breakerProcessList.add(dto);
				}
			}
		}
	}

	/**
	 * 标签页切换事件
	 *
	 * @param event
	 */
	public void onTabChange(TabChangeEvent event) {
		String title = event.getTab().getTitle();
		if (firstStationDetail != null && title.equals(firstStationDetail.getStationName()))
			currentSelectStation = firstStationDetail.getStationId();
		else if (secondStationDetail != null && title.equals(secondStationDetail.getStationName()))
			currentSelectStation = secondStationDetail.getStationId();
	}

	/**
	 * 设备选择器选择后，填补数据
	 *
	 * @param event
	 */
	public void onDeviceDialogReturn() {
		String data = getParameter("data");
		String className = getParameter("className");
		String type = getParameter("type");
		JSONObject equipment = JSONObject.parseObject(data);
		DeviceActionProcessDTO dto = new DeviceActionProcessDTO();
		if (equipment != null) {
			dto.setId(equipment.getString("id"));
			if ("zbhdz".equals(type) && ProtectionEquipment.class.getSimpleName().equals(className)) {
				ProtectionEquipment pe = JSONObject.parseObject(data, ProtectionEquipment.class);
				dto.setDevModel(pe.getDevType());
				dto.setDevType(zbh);
				dto.setDevName(pe.getType());
			} else if ("dlqbhdzxx".equals(type) && ProtectionEquipment.class.getSimpleName().equals(className)) {
				ProtectionEquipment pe = JSONObject.parseObject(data, ProtectionEquipment.class);
				dto.setDevModel(pe.getDevType());
				dto.setDevType(dlqbh);
				dto.setDevName(pe.getName());
			} else if ("dlqdzxx".equals(type) && Breaker.class.getSimpleName().equals(className)) {
				Breaker b = JSONObject.parseObject(data, Breaker.class);
				dto.setDevType(dlq);
				dto.setDevName(b.getName());
			}
		} else {
			return;
		}
		boolean needAdd = true;
		if (currentSelectStation.equals(firstStationDetail.getStationId())) {
			if ("dlqdzxx".equals(type)) {
				for (DeviceActionProcessDTO d : firstBreakerDeviceActionProcessList) {
					if (d.getId().equals(dto.getId())) {
						needAdd = false;
					}
				}
				if (needAdd)
					firstBreakerDeviceActionProcessList.add(dto);
			} else {
				for (DeviceActionProcessDTO d : firstProtectDeviceActionProcessList) {
					if (d.getId().equals(dto.getId())) {
						needAdd = false;
					}
				}
				if (needAdd)
					firstProtectDeviceActionProcessList.add(dto);
				Collections.sort(firstProtectDeviceActionProcessList, processComparator);
			}
		} else if (currentSelectStation.equals(secondStationDetail.getStationId())) {
			if ("dlqdzxx".equals(type)) {
				for (DeviceActionProcessDTO d : secondBreakerDeviceActionProcessList) {
					if (d.getId().equals(dto.getId())) {
						needAdd = false;
					}
				}
				if (needAdd)
					secondBreakerDeviceActionProcessList.add(dto);
			} else {
				for (DeviceActionProcessDTO d : secondProtectDeviceActionProcessList) {
					if (d.getId().equals(dto.getId())) {
						needAdd = false;
					}
				}
				if (needAdd)
					secondProtectDeviceActionProcessList.add(dto);
				Collections.sort(secondProtectDeviceActionProcessList, processComparator);
			}
		}
		// 更新前台表格，不知为何这里从前台无法直接更新表格
		PrimeFaces.current().ajax().update(Arrays.asList("form1:stations:fdt", "form1:stations:fbdt",
				"form1:stations:sdt", "form1:stations:sbdt"));
	}

	/**
	 * 删除第一个站选择的 保护/断路器动作详情
	 */
	public void deleteFirstSelectActionProcess() {
		if (firstSelectedProtectDeviceActionProcessList != null) {
			for (DeviceActionProcessDTO dto : firstSelectedProtectDeviceActionProcessList) {
				if (zbh.equals(dto.getDevType())) {
					fpm.deleteFaultReportMainProtection(firstStationDetail, dto.getId());
				} else if (dlqbh.equals(dto.getDevType())) {
					fpm.deleteFaultReportBreakerProtection(firstStationDetail, dto.getId());
				}
				firstProtectDeviceActionProcessList.remove(dto);
			}
		}
		if (firstSelectedBreakerDeviceActionProcessList != null) {
			for (DeviceActionProcessDTO dto : firstSelectedBreakerDeviceActionProcessList) {
				fpm.deleteFaultReportBreaker(firstStationDetail, dto.getId());
				firstBreakerDeviceActionProcessList.remove(dto);
			}
		}
	}

	/**
	 * 删除第二个站选择的 保护/断路器动作详情
	 */
	public void deleteSecondSelectActionProcess() {
		if (secondSelectedProtectDeviceActionProcessList != null) {
			for (DeviceActionProcessDTO dto : secondSelectedProtectDeviceActionProcessList) {
				if (zbh.equals(dto.getDevType())) {
					fpm.deleteFaultReportMainProtection(secondStationDetail, dto.getId());
				} else if (dlqbh.equals(dto.getDevType())) {
					fpm.deleteFaultReportBreakerProtection(secondStationDetail, dto.getId());
				}
				secondProtectDeviceActionProcessList.remove(dto);
			}
		}
		if (secondSelectedBreakerDeviceActionProcessList != null) {
			for (DeviceActionProcessDTO dto : secondSelectedBreakerDeviceActionProcessList) {
				fpm.deleteFaultReportBreaker(secondStationDetail, dto.getId());
				secondBreakerDeviceActionProcessList.remove(dto);
			}
		}
	}

	/**
	 * 删除第一个站选择的 动作元件详情
	 */
	public void deleteFirstSelectComponentList() {
		if (firstSelectComponentList != null) {
			for (FaultReportComponentDetail detail : firstSelectComponentList) {
				if (StringUtils.isEmpty(detail.getId())) {
					continue;
				}
				List<FaultReportComponentDetail> list = new ArrayList<FaultReportComponentDetail>();
				detail.setStatus(-1);
				list.add(detail);
				fpm.saveFaultReportComponentDetail(firstStationDetail, list);
				firstComponentList.remove(detail);
				firstStationDetail.getFaultReportComponentDetails().remove(detail);
			}
		}
	}

	/**
	 * 删除第二个站选择的 保护/断路器动作详情
	 */
	public void deleteSecondSelectComponentList() {
		if (secondSelectComponentList != null) {
			for (FaultReportComponentDetail detail : secondSelectComponentList) {
				if (StringUtils.isEmpty(detail.getId())) {
					continue;
				}
				List<FaultReportComponentDetail> list = new ArrayList<FaultReportComponentDetail>();
				detail.setStatus(-1);
				list.add(detail);
				fpm.saveFaultReportComponentDetail(secondStationDetail, list);
				secondComponentList.remove(detail);
				secondStationDetail.getFaultReportComponentDetails().remove(detail);
			}
		}
	}

	/**
	 * 审核人选择回掉
	 *
	 * @param event
	 */
	public void onAuditorDialogReturn() {
		PersonnelEntityBean user = getCurrentUser();
		if (StringUtils.isNotEmpty(currentSelectStation)) {
			if (currentSelectStation.equals(firstStationDetail.getStationId())) {
				firstStationDetail.setReviewer(user.getName());
			} else if (currentSelectStation.equals(secondStationDetail.getStationId())) {
				secondStationDetail.setReviewer(user.getName());
			}
		}
		// }
	}

	/**
	 * 上传附件
	 */
	public void handleFileUpload(FileUploadEvent event) {
		handleFileUpload(event, currentSelectStation);
	}

	/**
	 * 下载附件
	 */
	public void downloadAttachment() {
		file = getAttachmentFile(currentSelectStation);
		if (file == null) {
			addErrorMessage("该文件不存在");
		}
	}

	/**
	 * 删除附件
	 */
	public void deleteAttachment() {
		deleteAttachment(currentSelectStation);
		loadAttachmentList(faultReportMain.getFaultRecord(), stationId1, stationId2);
	}

	public void onUserDialogReturn() {
		PersonnelEntityBean user = getCurrentUser();
		DepartmentEntityBean group = user.getGroup();
		if (StringUtils.isNotEmpty(currentSelectStation)) {
			if (currentSelectStation.equals(firstStationDetail.getStationId())) {
				firstStationDetail.setReportPerson(user.getName());
				firstStationDetail.setReportUnit(group.getName());
			} else if (currentSelectStation.equals(secondStationDetail.getStationId())) {
				secondStationDetail.setReportPerson(user.getName());
				secondStationDetail.setReportUnit(group.getName());
			}
		}
	}

	/**
	 * 变电站编辑
	 * 
	 * @param stationId
	 */
	public void editStation(String stationId) {
		// 如果该资源可用
		if (checkEditPageIsUsed(faultId + stationId)) {
			if (stationId.equals(firstStationDetail.getId())) {
				firstStationEdit = true;
			} else if (stationId.equals(secondStationDetail.getId())) {
				secondStationEdit = true;
			}
		} else { // 如果该资源不可用
			addErrorMessage("变电站正在被【 " + gainOwner(faultId + stationId) + " 】编辑，请等待！");
		}
	}

	public FaultReportMain getFaultReportMain() {
		return faultReportMain;
	}

	public void setFaultReportMain(FaultReportMain faultReportMain) {
		this.faultReportMain = faultReportMain;
	}

	public FaultReportStationDetail getFirstStationDetail() {
		return firstStationDetail;
	}

	public void setFirstStationDetail(FaultReportStationDetail firstStationDetail) {
		this.firstStationDetail = firstStationDetail;
	}

	public FaultReportStationDetail getSecondStationDetail() {
		return secondStationDetail;
	}

	public void setSecondStationDetail(FaultReportStationDetail secondStationDetail) {
		this.secondStationDetail = secondStationDetail;
	}

	public boolean isShowFirstTableAllColumns() {
		return showFirstTableAllColumns;
	}

	public void setShowFirstTableAllColumns(boolean showFirstTableAllColumns) {
		this.showFirstTableAllColumns = showFirstTableAllColumns;
	}

	public boolean isShowSecondTableAllColumns() {
		return showSecondTableAllColumns;
	}

	public void setShowSecondTableAllColumns(boolean showSecondTableAllColumns) {
		this.showSecondTableAllColumns = showSecondTableAllColumns;
	}

	public List<FaultReportComponentDetail> getFirstComponentList() {
		return firstComponentList;
	}

	public void setFirstComponentList(List<FaultReportComponentDetail> firstComponentList) {
		this.firstComponentList = firstComponentList;
	}

	public List<FaultReportComponentDetail> getSecondComponentList() {
		return secondComponentList;
	}

	public void setSecondComponentList(List<FaultReportComponentDetail> secondComponentList) {
		this.secondComponentList = secondComponentList;
	}

	public List<DeviceActionProcessDTO> getFirstProtectDeviceActionProcessList() {
		return firstProtectDeviceActionProcessList;
	}

	public void setFirstProtectDeviceActionProcessList(
			List<DeviceActionProcessDTO> firstProtectDeviceActionProcessList) {
		this.firstProtectDeviceActionProcessList = firstProtectDeviceActionProcessList;
	}

	public List<DeviceActionProcessDTO> getSecondProtectDeviceActionProcessList() {
		return secondProtectDeviceActionProcessList;
	}

	public void setSecondProtectDeviceActionProcessList(
			List<DeviceActionProcessDTO> secondProtectDeviceActionProcessList) {
		this.secondProtectDeviceActionProcessList = secondProtectDeviceActionProcessList;
	}

	public List<DeviceActionProcessDTO> getFirstBreakerDeviceActionProcessList() {
		return firstBreakerDeviceActionProcessList;
	}

	public void setFirstBreakerDeviceActionProcessList(
			List<DeviceActionProcessDTO> firstBreakerDeviceActionProcessList) {
		this.firstBreakerDeviceActionProcessList = firstBreakerDeviceActionProcessList;
	}

	public List<DeviceActionProcessDTO> getSecondBreakerDeviceActionProcessList() {
		return secondBreakerDeviceActionProcessList;
	}

	public void setSecondBreakerDeviceActionProcessList(
			List<DeviceActionProcessDTO> secondBreakerDeviceActionProcessList) {
		this.secondBreakerDeviceActionProcessList = secondBreakerDeviceActionProcessList;
	}

	public List<DeviceActionProcessDTO> getFirstSelectedProtectDeviceActionProcessList() {
		return firstSelectedProtectDeviceActionProcessList;
	}

	public void setFirstSelectedProtectDeviceActionProcessList(
			List<DeviceActionProcessDTO> firstSelectedProtectDeviceActionProcessList) {
		this.firstSelectedProtectDeviceActionProcessList = firstSelectedProtectDeviceActionProcessList;
	}

	public List<DeviceActionProcessDTO> getSecondSelectedProtectDeviceActionProcessList() {
		return secondSelectedProtectDeviceActionProcessList;
	}

	public void setSecondSelectedProtectDeviceActionProcessList(
			List<DeviceActionProcessDTO> secondSelectedProtectDeviceActionProcessList) {
		this.secondSelectedProtectDeviceActionProcessList = secondSelectedProtectDeviceActionProcessList;
	}

	public List<DeviceActionProcessDTO> getFirstSelectedBreakerDeviceActionProcessList() {
		return firstSelectedBreakerDeviceActionProcessList;
	}

	public void setFirstSelectedBreakerDeviceActionProcessList(
			List<DeviceActionProcessDTO> firstSelectedBreakerDeviceActionProcessList) {
		this.firstSelectedBreakerDeviceActionProcessList = firstSelectedBreakerDeviceActionProcessList;
	}

	public List<DeviceActionProcessDTO> getSecondSelectedBreakerDeviceActionProcessList() {
		return secondSelectedBreakerDeviceActionProcessList;
	}

	public void setSecondSelectedBreakerDeviceActionProcessList(
			List<DeviceActionProcessDTO> secondSelectedBreakerDeviceActionProcessList) {
		this.secondSelectedBreakerDeviceActionProcessList = secondSelectedBreakerDeviceActionProcessList;
	}

	public List<FaultReportComponentDetail> getFirstSelectComponentList() {
		return firstSelectComponentList;
	}

	public void setFirstSelectComponentList(List<FaultReportComponentDetail> firstSelectComponentList) {
		this.firstSelectComponentList = firstSelectComponentList;
	}

	public List<FaultReportComponentDetail> getSecondSelectComponentList() {
		return secondSelectComponentList;
	}

	public void setSecondSelectComponentList(List<FaultReportComponentDetail> secondSelectComponentList) {
		this.secondSelectComponentList = secondSelectComponentList;
	}

	public String getCurrentSelectStation() {
		return currentSelectStation;
	}

	public void setCurrentSelectStation(String currentSelectStation) {
		this.currentSelectStation = currentSelectStation;
	}

	public FaultRecord getFr() {
		return fr;
	}

	public void setFr(FaultRecord fr) {
		this.fr = fr;
	}

	public boolean isFirstStationEdit() {
		return firstStationEdit;
	}

	public void setFirstStationEdit(boolean firstStationEdit) {
		this.firstStationEdit = firstStationEdit;
	}

	public boolean isSecondStationEdit() {
		return secondStationEdit;
	}

	public void setSecondStationEdit(boolean secondStationEdit) {
		this.secondStationEdit = secondStationEdit;
	}

}
